import PAGE from '../../utils/middleware/page'
Page(PAGE({
    data: {
        latitude: 30.67,
        longitude: 104.06,
        scale: 13,
        markers: [],
        circles: [],
        controls: [],
        loaded: false,
        list: [],
        apiField: {
            module: 'common',
            api: 'ParkList',
            data: 'list',
            search: {
                Keywords: '',
                Longitude: null,//经度
                Latitude: null //纬度
            }
        },
        selectInfo: null
    },
    onLoad(options) {
        this.getSetting()
    },
    onPullDownRefresh() {

    },
    fetchData() {
        var _this = this
        this.handleFetchData({
            complete(data) {
                var i = -1;
                var markers = data.map(item => {
                    i++
                    return {
                        id: i,
                        width: 48,
                        height: 48,
                        longitude: item.Longitude,
                        latitude: item.Latitude,
                        iconPath: '/assets/images/icons/location3.png'
                    }
                })

                _this.setData({
                    markers: markers
                })
            }
        })
    },
    handleMarkerTap(marker) {
        console.log(marker)
        this.setData({
            selectInfo: this.data.list[marker.markerId]
        })
    },
    getLocation() {
        var that = this
        wx.getLocation({
            type: 'gcj02',
            success(res) {
                console.log(res)
                that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    circles: [{
                        latitude: res.latitude,
                        longitude: res.longitude,
                        // color: '#FF0000DD',
                        fillColor: '#7cb5ec88',
                        radius: 3000,
                        // strokeWidth: 0
                    }]
                })

                that.data.apiField.search.Longitude = res.longitude
                that.data.apiField.search.Latitude = res.latitude

                that.fetchData()
            },
            fail(res) {
                wx.showToast({
                    title: '授权失败',
                    icon: 'success',
                    duration: 1000
                })
            }
        })
    },
    getSetting() {
        var that = this
        wx.getSetting({
            success: (res) => {
                if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
                    wx.showModal({
                        title: '是否授权当前位置',
                        content: '将获取您的位置用于显示附近车库信息，请确认授权！',
                        success: function (res) {
                            if (res.cancel) {
                                wx.showToast({
                                    title: '授权失败',
                                    icon: 'success',
                                    duration: 1000
                                })
                            } else if (res.confirm) {
                                wx.openSetting({
                                    success: function (dataAu) {
                                        if (dataAu.authSetting["scope.userLocation"] == true) {
                                            wx.showToast({
                                                title: '授权成功',
                                                icon: 'success',
                                                duration: 1000
                                            })
                                            //再次授权，调用getLocationt的API
                                            that.getLocation();
                                        } else {
                                            wx.showToast({
                                                title: '授权失败',
                                                icon: 'success',
                                                duration: 1000
                                            })
                                        }
                                    }
                                })
                            }
                        }
                    })
                } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
                    that.getLocation();
                }
                else { //授权后默认加载
                    that.getLocation();
                }
            }
        })
    },
    handleOpenLocation() {
        wx.openLocation({
            latitude: this.data.selectInfo.Latitude,
            longitude: this.data.selectInfo.Longitude
        })
    }
}))